﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoGame.Model;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
using SoGame.Helper;

namespace SoGame.ViewModels
{
    public class TopPlayersViewModel : BaseViewModel
    {
        private SoGameDbContext context = new SoGameDbContext();
        private ObservableCollection<GamePlayViewModel2> top10Scores = new ObservableCollection<GamePlayViewModel2>();

        public TopPlayersViewModel() {
            context.Configuration.LazyLoadingEnabled = true;

            LoadPlayers("SOSLAM");
        }

        private void LoadPlayers(string game)
        {
            this.top10Scores.Clear();
            int index = 1;
            foreach (var item in context.GetTop10Score(game))
            {
                top10Scores.Add(new GamePlayViewModel2(index, item));
                index ++;
            }

            OnPropertyChanged(() => Player1);
            OnPropertyChanged(() => Player2);
            OnPropertyChanged(() => Player3);
        }

        public ObservableCollection<GamePlayViewModel2> Top10Scores
        {
            get
            {
                return top10Scores;
            }
        }

        public GamePlay Player1
        {
            get
            {
                if (this.top10Scores.Any())
                {
                    return this.Top10Scores[0].GamePlay;
                }
                else {
                    return null;
                }
            }
        }

        public GamePlay Player2
        {
            get
            {
                if (this.top10Scores.Any() && this.top10Scores.Count > 1)
                {
                    return this.Top10Scores[1].GamePlay;
                }
                else
                {
                    return null;
                }
            }
        }

        public GamePlay Player3
        {
            get
            {
                if (this.top10Scores.Any() && this.top10Scores.Count > 2)
                {
                    return this.Top10Scores[2].GamePlay;
                }
                else
                {
                    return null;
                }
            }
        }

        #region ICommand
        public ICommand GameSelection
        {
            get { return new RelayCommand<string>(ExecuteGameSelection, CanExecuteNavigateCommand); }
        }

        public void ExecuteGameSelection(string command) {
            this.LoadPlayers(command);
        }

        public bool CanExecuteNavigateCommand(string param)
        {
            return true;
        }
        #endregion
    }

    public class GamePlayViewModel2 : BaseViewModel
    {
        private int _postion;
        public int Position
        {
            get { return _postion; }
            set
            {
                _postion = value;
                this.OnPropertyChanged(() => this.Position);
            }
        }

        private GamePlay _gamePlay;
        public GamePlay GamePlay
        {
            get { return _gamePlay; }
            set
            {
                _gamePlay = value;
                this.OnPropertyChanged(() => this.GamePlay);
            }
        }

        public GamePlayViewModel2(int position, GamePlay gameplay)
        {
            Position = position;
            GamePlay = gameplay;
        }
    }
}
